/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CadastroNivelsDialog.java
 *
 * Created on 07/11/2009, 19:47:30
 */
package interpretadorportugolvisual.cadastros;

import br.org.isvi.interpretador.portugol.access.IPVBusinessFacade;
import br.org.isvi.interpretador.portugol.access.NivelDAO;
import br.org.isvi.interpretador.portugol.model.Nivel;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author abraaoisvioliveirasouzadocarmo
 */
public class CadastroNiveisDialog extends javax.swing.JDialog {

    /** Creates new form CadastroNivelsDialog */
    public CadastroNiveisDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();

//        tblNivels.getModel().addTableModelListener(this);
        tblNivels.setCellSelectionEnabled(true);
        tblNivels.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btnSair = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblNivels = new javax.swing.JTable();
        btnAdicionar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jPanel1.setName("jPanel1"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(interpretadorportugolvisual.InterpretadorPortugolVisualApp.class).getContext().getResourceMap(CadastroNiveisDialog.class);
        btnSair.setText(resourceMap.getString("btnSair.text")); // NOI18N
        btnSair.setName("btnSair"); // NOI18N
        btnSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSairActionPerformed(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tblNivels.setModel(obterNivels());
        tblNivels.setName("tblNivels"); // NOI18N
        jScrollPane1.setViewportView(tblNivels);

        btnAdicionar.setText(resourceMap.getString("btnAdicionar.text")); // NOI18N
        btnAdicionar.setName("btnAdicionar"); // NOI18N
        btnAdicionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAdicionarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(btnAdicionar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 275, Short.MAX_VALUE)
                        .addComponent(btnSair))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 557, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSair)
                    .addComponent(btnAdicionar))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAdicionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAdicionarActionPerformed
        String result = JOptionPane.showInputDialog("Descrição da novo Nível");
        Nivel t = new NivelDAO().novo();
        t.setDescricao(result);

        IPVBusinessFacade.persistir();

        ((DefaultTableModel) tblNivels.getModel()).addRow(new Nivel[]{t});
    }//GEN-LAST:event_btnAdicionarActionPerformed

    private void btnSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSairActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_btnSairActionPerformed


    public DefaultTableModel obterNivels() {
        java.util.List<Nivel> lst = new ArrayList<Nivel>();
        try {
            lst = IPVBusinessFacade.obterTodosNiveis();
        } catch(Exception e) {

        }

        Nivel[][] Nivels = new Nivel[lst.size()][1];

        for (int i = 0; i < Nivels.length; i++) {
            Nivels[i] = new Nivel[]{lst.get(i)};
        }


        return new DefaultTableModel(Nivels, new String[]{"Descrição"}) {

            @Override
            public void setValueAt(Object arg0, int arg1, int arg2) {
                Nivel t = (Nivel) getValueAt(arg1, arg2);

                t.setDescricao(arg0.toString());
                IPVBusinessFacade.persistir();

                fireTableCellUpdated(arg1, arg2);
            }
        };
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdicionar;
    private javax.swing.JButton btnSair;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblNivels;
    // End of variables declaration//GEN-END:variables

}
